#!/usr/bin/bash
# Copyright (c) 2023. huawei Technologies Co.,Ltd.ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
#          http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# #############################################
# @Author    :   zhuwenshuo
# @Contact   :   1003254035@qq.com
# @Date      :   2023/02/21
# @License   :   Mulan PSL v2
# @Desc      :   Test autotest
# #############################################
source "$OET_PATH/libs/locallibs/common_lib.sh"
function pre_test() {
    LOG_INFO "Start to prepare the test environment."
    DNF_INSTALL "rubygem-ZenTest tar rubygem-test-unit"
    tar -xvf ./common/data.tar.gz >/dev/null
    multigem
    LOG_INFO "End to prepare the test environment."
}
function run_test() {
    LOG_INFO "Start to run test."
    unit_diff -h 2>&1 | grep -e "test.rb | unit_diff \[options]"
    CHECK_RESULT $? 0 0 "Check unit_diff -h failed"
    unit_diff -v | grep "$(rpm -qa rubygem-ZenTest | awk -F '-' '{print $3}')"
    CHECK_RESULT $? 0 0 "Check unit_diff -v failed"
    zentest ./data/testvideostore.rb ./data/videostore.rb | unit_diff -b | grep "classname: asrt"
    CHECK_RESULT $? 0 0 "Check unit_diff -b failed"
    zentest ./data/testvideostore.rb ./data/videostore.rb | unit_diff -c | grep "Number of errors detected"
    CHECK_RESULT $? 0 0 "Check unit_diff -c failed"
    zentest ./data/testvideostore.rb ./data/videostore.rb | unit_diff -k | grep "3 errors"
    CHECK_RESULT $? 0 0 "Check unit_diff -k failed"
    zentest ./data/testvideostore.rb ./data/videostore.rb | unit_diff -u | grep "Code Generated by ZenTest"
    CHECK_RESULT $? 0 0 "Check unit_diff -u failed"
    zentest ./data/testvideostore.rb ./data/videostore.rb | unit_diff -p
    CHECK_RESULT $? 0 0 "Check unit_diff -p failed"
    multigem | grep "TOTAL RESULT"
    CHECK_RESULT $? 0 0 "Check multigem failed"
    multiruby | grep "TOTAL RESULT"
    CHECK_RESULT $? 0 0 "Check multiruby failed"
    LOG_INFO "End to run test."
}
function post_test() {
    LOG_INFO "Start to restore the test environment."
    DNF_REMOVE "$@"
    rm -rf tmp.txt lib/ data/
    LOG_INFO "End to restore the test environment."
}
main "$@"
